package PatientManagement

import (
	"MedicalTreatment/global"
	"MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func Patient(r *gin.Engine) {
	r.POST("/addPatient", func(context *gin.Context) {
		var userPatient model.Patient
		if err := context.ShouldBindJSON(&userPatient); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			fmt.Println(userPatient)
			return
		}
		var pat model.Patient
		if err := global.DB.Where("patient_id_card=?", userPatient.Patient_idCard).First(&pat).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				if err := global.DB.Create(&userPatient).Error; err != nil {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				} else {
					context.JSON(200, gin.H{
						"Msg": "创建成功",
					})
				}
			}
		} else {
			context.JSON(200, gin.H{
				"Msg": "患者重复",
			})
		}

	})
}
